/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ez2find.manager;

import com.ez2find.model.Image;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Owner
 */
@Stateless
public class ImageManagerBean implements ImageManagerLocal {
    
    @PersistenceContext
    private EntityManager em;
    
    public void create(Image image)throws Exception {
        em.persist(image);
    }
 
    public void update(Image image)throws Exception {
        em.merge(image);
    }
    
    public void delete(Image image)throws Exception {
        image=em.merge(image);
        em.remove(image);
    }
    
    public Image find(long id){
        return em.find(Image.class,id);
    }

    public List<Image> listAll(){
        Query query=em.createNamedQuery("listAllImages");
        List<Image> result=query.getResultList();
        return result;
    }
    
    public long lastCode(){
        try{
            Query query=em.createNamedQuery("lastImageCode");
            return (Long) query.getSingleResult();
        }catch(Exception ex){
            return 0;
        }
    }
}
